#ifndef COMPOSITE_BODY_H
#define COMPOSITE_BODY_H

/***************************************************************************
				CompositeBody.cpp
			       -------------------
Copyright (c) 2009-2010 
Author: Kishor D. Bhalerao (kishor8dm@gmail.com)
Contributors: 

This file is associated with the Ph.D. dissertation,"ON METHODS FOR EFFICIENT
AND ACCURATE DESIGN AND SIMULATION OF MULTIBODY SYSTEMS", Bhalerao K.D,
Rensselaer Polytechnic Institute, 2010.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1.) Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2.) Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

3.) The name of the authors, their employers, or sponsoring organizations may
not be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE AUTHORS, THEIR EMPLOYERS, OR SPONSORING ORGANIZATIONS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
***************************************************************************/

#include "SimTKsimbody.h"
#include "Simbody.h"
#include "DcaDataTypes.h"
#include "DcaPreprocessor.h"

using namespace SimTK;
class CompositeBody{
public:
	
	CompositeBody();
	
	///////////////////////////////////////////////////////////////////////////////
	// These functions are for equations of motion
	///////////////////////////////////////////////////////////////////////////////
	
	void expressEomDatainBasis(EomData& H,const DcaBodyData& body, const Vec6& psi13, const Vec6& psi23, Mat33& B_C_A);
	void expressEomDatainBasis(EomData& H,const CompositeBody& body, Mat33& B_C_A);
	void expressEomDatainBasis(EomData& H,Mat33& B_C_A);

	// Functions to form Composite body from different primitives
	// Todo: Write a function to form composite body from two existing composite bodies
	void FormCompositeBody(const int& ParentId, const int& ChildId,const int& CompositeId,
			       SimbodyMatterSubsystem& matter, State& state, TwoHandleEquations& THE,
				   const int **FreeJoints);
	
	void FormCompositeBody(CompositeBody& Parent, const int& ChildId,const int& CompositeId,
			       SimbodyMatterSubsystem&matter, State& state,TwoHandleEquations& THE,
			       const int **FreeJoints);	

	// Calculate X12. b1 is parent, b2 is child.
	void calcX12(const EomData& b1, const DcaBodyData& b2,const JointType& J, Mat66& X12);

	///////////////////////////////////////////////////////////////////////////////
	// Functions specific to projecting momenta
	///////////////////////////////////////////////////////////////////////////////
	
	void formCompositeBody_projectMom(const int& ParentId, const int& ChildId, const int& CompositeId, 
					  SimbodyMatterSubsystem& matter, State& state, const DcaPreprocessor& DcaObj,
					  int **FreeJoints);
					  
	void formCompositeBody_projectMom(CompositeBody& Parent, const int& ChildIdid, const int& CompositeId, 
						 SimbodyMatterSubsystem& matter, State& state,const DcaPreprocessor& DcaObj,
						 int **FreeJoints);					  
					  					  
	void calcX12_ProjectMom(const DcaBodyData_projectMom& b1, const DcaBodyData& b2,const JointType& JT, Mat66& X12);
	void calcX12_ProjectMom(const EomData& b1, const DcaBodyData& b2,const JointType& JT, Mat66& X12);
	
	///////////////////////////////////////////////////////////////////////////////
	
	
	const int getParent() const {return Parent_;}
	const int getChild() const {return Child_;}
	const int getCompositeBodyId() const {return CompositeBodyId_;}
	const EomData& getEomData() const {return DT_;}
	const Mat66& getA() const {return A_;}
	const Mat66& getB() const {return B_;}
	const Vec6& getC() const {return C_;}

private:
	EomData DT_;
	Mat66 A_,B_;
	Vec6 C_;
	int Parent_, Child_, CompositeBodyId_;
};

#endif
